/*
* Com_Interface.h
* Last edited: 27.08.2014
* Author: David Noack
*/

#ifndef BBS_H_
#define BBS_H_

#include <iostream>
#include <fstream>
#include <math.h>
#include <unistd.h>
#include <gmp.h>
#include <gmpxx.h>

#define FAST_PART_SIZE 2048


/*! \class BBS
	\brief Blum Blum Shub pseudo random number generator

	This class implements the Blum Blum Shub (bbs) prngd to create random bits.
	For big integer computation, the gnu gmp library is used.

*/
class BBS
{

	public:

		/** Constructor */
		BBS();

		/** Destructor */
		~BBS();

		/**
		Generates a number of random bits.
		\param s A initial big integer seed for bbs.
		\param bitCount The number of bits to generate.
		\return The generated random bits as char-array.
		*/
		char* generateBits(mpz_t s, int bitCount, bool fastmode);


	private:


		/** Generates the modul m (p*q) which is a private variable.
		 \param bitCount The number of needed random bits.
		 */
		void create_M(int bitCount);


		/** Helpfunction for debugging which prints a char-array of to std::out.
		 \param data Pointer to the data which has to be printed.
		 \param size Size of the data(-array).
		 \param binary If true, every character of the data will be printed as 8 chars of ones and zeros.
		 */
		void printRndData(char* data, int size, bool binary);


		/** Generate a blum prime ( 4k+3 )
		 \param bigInt A big integer variable to save the generated blum prime.
		 \param bits Size in bits for the blum prime.
		 */
		void getBlumPrime(mpz_t bigInt, int bits);

		/** First blumprime (3mod4) which is part of m*/
		mpz_t p;
		/** Second blumprime (3mod4) whis is part of m*/
		mpz_t q;
		/** The modul  M (= p * q)*/
		mpz_t m;

		/** Temp var for intern computations*/
		mpz_t r;
		/** Temp var for intern computations*/
		mpz_t output;
		/** Final var which contains all the generated rando bits of the bbs*/
		mpz_t output2; 
		/** Contains the random bits of output2 as char-array (8bit per char) and is given back as return value*/
		char* byteOutput;
		/** If this is true, the generator will TRY to use /dev/random*/
		bool useRandom; 
		mpz_t newSeed;

};
#endif


